package zhoug.common.view.dialog;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;

import zhoug.common.R;

import androidx.annotation.NonNull;

/**
 * 正在加载圆形窗口
 * @Author 35574
 * @Date 2020/7/21
 * @Description
 */
public class ZLoadingDialog extends ZDialog {
    private static final String TAG = ">>>ZLoadingDialog";
    private static final boolean DEBUG = false;
    private ProgressBar mProgressBar;
    private Drawable mLoadingDrawable;

    public ZLoadingDialog(@NonNull Context context) {
        super(context,0, R.attr.ZLoadingDialog_theme, R.style.ZLoadingDialog_def);
        setCanceledOnTouchOutside(false);
    }

    @Override
    protected void initialize(@NonNull Context context, int defStyleAttr, int defStyleRes) {
        super.initialize(context, defStyleAttr, defStyleRes);
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZLoadingDialog, defStyleAttr, defStyleRes);
        int width = a.getLayoutDimension(R.styleable.ZLoadingDialog_android_layout_width, WindowManager.LayoutParams.WRAP_CONTENT);
        int height = a.getLayoutDimension(R.styleable.ZLoadingDialog_android_layout_height, WindowManager.LayoutParams.WRAP_CONTENT);
        mLoadingDrawable = a.getDrawable(R.styleable.ZLoadingDialog_zg_loadingDrawable);
        setAttributes(width, height, Gravity.CENTER);
        a.recycle();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.common_z_loading_dialog;
    }

    @Override
    protected void onViewCreated(Bundle savedInstanceState) {
        mProgressBar = findViewById(R.id.progressBar);
        ViewGroup.LayoutParams layoutParams = mProgressBar.getLayoutParams();
        if (layoutParams != null) {
            layoutParams.width = getWidth();
            layoutParams.height = getHeight();
            mProgressBar.setLayoutParams(layoutParams);
        }
        if (mLoadingDrawable != null) {
            mProgressBar.setIndeterminateDrawable(mLoadingDrawable);
        }

        Window window = getWindow();
        if (window != null) {
            if (DEBUG) {
                Log.d(TAG, "init:去掉背景");
            }
            window.setBackgroundDrawableResource(R.drawable.common_transparent);
        }
    }

    public void setLoadingDrawable(Drawable mLoadingDrawable) {
        this.mLoadingDrawable = mLoadingDrawable;
    }
}
